package com.horse.cloud.framework.base.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @create : Created by intelliJ IDEA 17.2
 * @author : bing.Pan
 * @e-mail : 15923508369@163.com
 * @gmdate : 02 17 2018 22:27
 * @sidesc : feign服务端请求、响应日志记录 aspect
 */


@Aspect
@Component
public class ForeignServiceLogAspect {

    protected Logger logger =  LoggerFactory.getLogger(ForeignServiceLogAspect.class);

    @Pointcut("execution(public * com.horse.cloud.framework.base.BaseFeignService+.*(..))")
    public void foreignServiceLog(){}

    @Before("foreignServiceLog()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();


        logger.info("【framework-base】统一feignService日志记录,接口地址:[{}]; 请求地址:[{}]; 请求方式:[{}];执行方法:[{}]; 携带参数:[{}]",
                request.getRequestURL().toString(),
                request.getRemoteAddr(),request.getMethod(),
                joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName(),
                Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(returning = "ret", pointcut = "foreignServiceLog()")
    public void doAfterReturning(Object ret) {
        logger.info("【framework-base】统一feignService日志记录,返回内容:[{}]",ret);
    }

}
